// pages/wallet/tixian.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js");
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payMoney:0,
    accountId:0,
    splitMoney:.0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
        this.data.accountId = parseInt(options.id)
    }
    if(options.money){
      this.data.splitMoney = options.money;
      this.setData({
        splitMoney: this.data.splitMoney
      })
    }
  },
  inputMoney(e){
    console.log('inputMoney',e);
    var money = e.detail.value;
    if(money == ""){
      money = "0";
    }
    let value = e.detail.value
    if (value != '') {
      value = value.replace(/[^\d.]/g, "")
      if (/^(\d?)+(\.\d{0,2})?$/.test(value)) {} else {
        value = value.substring(0, value.length - 1);
      }
      value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
      if (value.indexOf(".") < 0 && value != "") {
        value = parseFloat(value);
      }
      if (Number(value) > 200) {
        value = 200
      }
      if (Number(value) < 1) {
        value = 1
      }
    }
    console.log('value',value)
    this.data.payMoney = value;
    this.setData({
      payMoney: value
    })
  },
  handlePhoneNum(){
    var that = this;
    var message = "请先绑定手机号后再提现哟~"
    Dialog.confirm({
      title: '温馨提醒',
      message
    }).then(() => {
      wx.navigateTo({
        url: '/pages/account/index?from_type=tixian',
      })
    })
    .catch(() => {
      Dialog.close();
    });
  },
  goPayCash(){
    // Toast.success("提现功能暂未开放，敬请期待...")
    var phoneNum = app.getStorageSync("phone_num");
    console.log('phoneNum',phoneNum)
    if(phoneNum == undefined || phoneNum== ''){
      this.handlePhoneNum();
    }else{
      this.onHttpPayCash();
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpPayCash: function () {
    if(this.data.payMoney == 0){
      Toast.fail("提现金额必须大于1");
      return;
    }
      var that = this
      fetchApi.httpPost('/account/add/cash/record/bingge', {
        account_id:that.data.accountId,
        pay_money:that.data.payMoney,
      }, function (res) {
        console.log("提现", res)
        if(res.data.code == 0){
          Toast.success(res.data.msg)
          setTimeout(function(){
            wx.navigateBack({
              delta: -1,
            })
          },500)
        }else{
          Toast.fail(res.data.msg)
        }
        }, function () {
          that.setData({ netWork: false, hasData: false })
      })
  },
})